#!/bin/bash

# 定义检测pip源的函数
check_pip_sources() {
    # 定义多个pip源
    local PIP_SOURCES=(
        "https://pypi.org/simple"
        "https://pypi.tuna.tsinghua.edu.cn/simple"
        "https://mirrors.aliyun.com/pypi/simple"
        "https://pypi.tuna.tsinghua.edu.cn/simple/"
        "http://pypi.mirrors.ustc.edu.cn/pypi/simple/"
        "https://mirrors.cloud.tencent.com/pypi/simple"
        "https://repo.huaweicloud.com/repository/pypi/simple/"
        "https://mirrors.163.com/pypi/simple/"
        "https://pypi.douban.com/simple/"
        "https://mirror.baidu.com/pypi/simple/"
    )

    # 临时文件用于测试pip源
    local TEMP_FILE=$(mktemp)

    # 检测函数
    check_pip_source() {
        local source=$1
        pip install requests -i $source --no-cache-dir > $TEMP_FILE 2>&1
        if grep -q "Successfully installed" $TEMP_FILE; then
            echo $source
            return 0
        fi
        return 1
    }

    # 遍历pip源并检测
    for source in "${PIP_SOURCES[@]}"; do
        echo "Checking $source..."
        if check_pip_source $source; then
            echo "Available source: $source"
            rm $TEMP_FILE
            return 0
        fi
    done

    # 如果所有源都不可用
    echo "No available pip sources found."
    rm $TEMP_FILE
    return 1
}

# 调用函数（示例）
#check_pip_sources
